layui.use(["layer","table","jquery","laytpl"],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var data;
    $.ajaxSettings.async = false;
    $.ajax({
        type: "get",
        url: "/listCar",
        success: function (obj) {
            data = obj;
        }
    });
    let newData = JSON.parse(JSON.stringify(data));
    var carTable = table.render({
        elem: "#carTable",
        data: newData,
        page: true,
        autoSort: false,
        cols: [[
            {field: "carId",title:"车牌号"},
            {field: "carType",title:"车类型", templet: function (data) {
                return data.carType===1?"大型车":"小型车"
            }},
            {field: "enterTime",title:"进入时间", sort: true, templet: function (data) {
                    let date = new Date(data.enterTime);
                    return date.toLocaleDateString()+" "+date.getHours()+":"+date.getMinutes()
                }},
            {field: "image", title:"图片", toolbar: "#actions"}
            ]]
    });
    table.on("tool(carTable)", function (obj) {
        console.info(obj);
        if(obj.event==="checkImage"){
            $("#imageLayer").d = "sss";
            if(obj.data.enterImg !== undefined){
                layer.open({
                    type: 1,
                    title: "图片",
                    skin: 'layui-layer-demo', //样式类名
                    anim: 2,
                    area: ['700px', '445px'],
                    shadeClose: true, //开启遮罩关闭
                    content: `<img height='400px' width='700px' src='../img/carImg/${obj.data.enterImg}'/>`
                });
            }else {
                layer.msg("本记录未保存图片");
            }
        }
    });
    table.on("sort(carTable)", function (obj) {
        console.info("ssss");
        if (obj.type === "desc"){ //降序
            let newData = JSON.parse(JSON.stringify(data)); //为了不改变原始排序，排序时拷贝对象
            newData.sort(function (data1,data2) { //data1和data2是冒泡排序的左右值
                return data2.enterTime-data1.enterTime;
            });
            carTable.reload({
                data: newData
            })
        }else if (obj.type === "asc"){ //升序
            let newData = JSON.parse(JSON.stringify(data)); //为了不改变原始排序，排序时拷贝对象
            newData.sort(function (data1,data2) { //data1和data2是冒泡排序的左右值
                return data1.enterTime-data2.enterTime;
            });
            carTable.reload({
                data: newData
            })
        }else if (obj.type === null) { //原始顺序
            let newData = JSON.parse(JSON.stringify(data));
            carTable.reload({
                data: newData
            });
        }
    });
    $("#findCarBtn").on("click",function () {
        $.ajax({
            url:"/findCar",
            data:{
                carNo: $("#findCarInput").val()
            },
            success: function (res) {
                console.info(typeof res);
                console.info(res);
                if (res[0] === null ||res.length === 0){
                    layer.msg("请输入正确的车牌号");
                }else{
                    console.info(res);
                    carTable.reload({
                        data: res
                    });
                }
            }
        })
    });
    $("#resetTable").on("click",function () {
        let newData = JSON.parse(JSON.stringify(data));
        carTable.reload({
            data: newData
        });
    });
});